1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/python
# -*- coding: GBK -*-
#-------------------------------------------------------------------------------
# @copyright: Copyright (c) 2004, 2010 Xiamen Wangyou Network Technology Co.,Ltd., All rights reserved.
#-------------------------------------------------------------------------------
#
 
import sys
import binascii
import base64
try:
    import threading
except ImportError:
    import dummy_threading as threading     
import CommFunc    
import MongoDBConfig  
#from Common import (CommFuncEx, CommFunc, mylog)
#from DBCommon import (CommonDefine, )
import traceback
 
PLAYERID_FEED = 10000
PLAYERID_STEP = 1
 
SIDErrorCnt = 0
MAGIC_SID = 2266520
 
SIDErrorCounterLock = threading.Lock()
 
ADOExceptionCnt = 0
AdoExceptionCounterLock = threading.Lock()
 
#ÐèÒª¶àÏ̱߳£»¤
def addADOExceptionCount():
    global ADOExceptionCnt
    global AdoExceptionCounterLock
    AdoExceptionCounterLock.acquire()
    ADOExceptionCnt += 1
    AdoExceptionCounterLock.release()
    return ADOExceptionCnt
 
def getADOExceptionCount():
    return ADOExceptionCnt
        
def fix_outgoingText(text):
    if isinstance(text, unicode):
        if MongoDBConfig.USER_DB_IsBase64:
             return base64.b64decode(text)
        return CommFunc.UnicodeToEncoding(MongoDBConfig.USER_DB_ENCODING, text)[1]
    #²»ÊÇunicode,²»´¦Àí
    return text
 
def fix_incomingText(text):
    if isinstance(text, str):
        if MongoDBConfig.USER_DB_IsBase64:
            return base64.b64encode(text)
        return CommFunc.EncodingToUnicode(MongoDBConfig.USER_DB_ENCODING, text)[1]
    #²»ÊÇ×Ö·û´®£¬²»´¦Àí
    return text
 
def fix_incoming(dictObj):
    for k, v in dictObj.iteritems():
        if isinstance(v, str):
            result, convertText = CommFuncEx.EncodingToUnicode(CommonDefine.ENCODING, v)
            if result:
                dictObj[k] = convertText
 
def fix_outgoing(dictObj):
    for k, v in dictObj.iteritems():
        if isinstance(v, unicode):
            result, convertText = CommFuncEx.UnicodeToEncoding(CommonDefine.ENCODING, v)
            if result:
                dictObj[k] = convertText
 
def addSIDErrorCnt():
    global SIDErrorCnt
    global SIDErrorCounterLock
    SIDErrorCounterLock.acquire()
    SIDErrorCnt += 1
    SIDErrorCounterLock.release()
    return SIDErrorCnt
 
def getSIDErrorCnt():
    global SIDErrorCnt
    return SIDErrorCnt
 
def makeSID(data):
#    mylog.debug('makeSID(%s) = %s'%(data, binascii.crc32(data)))
    return binascii.crc32(data)
 
def checkSID(data, expectSID):
    global MAGIC_SID
    if expectSID == MAGIC_SID:
        return True
    calcSID = makeSID(data)
    if expectSID == calcSID:
        return True
#    mylog.debug('expectSID = %s calcSID = %s'%(expectSID, calcSID))
    return False
 
SPLITCHAR = '\t'
 
def sysDBLoadFromFile(className, fileName):
    data = ''
    try:
        fileObj = open(fileName, 'rb', 0)
        mylog.info('Loading %s...'%fileName)
        try:
            rowObj = className()
            rowCnt = 0
            for line in fileObj:
                rowCnt += 1
                if rowCnt == 1:
                    continue    #skip header
#                logging.getLogger('sysDBLoadFromFile').debug('line %d = %s'%(rowCnt,line))
                rowObj.readLine(line)
                data += rowObj.getBuffer()
        except IOError,(readErrNo, readStdErr):
            mylog.error('Read %s failed! errno = %s stdErr = %s'%(fileName, readErrNo, readStdErr))
            data = ''
            data = CommFunc.WriteDWORD(data, 0)
            return data
        except:
            mylog.error('Exception = %s'%sys.exc_info()[0])
            mylog.error(traceback.format_exc()) 
            data = ''
            data = CommFunc.WriteDWORD(data, 0)
            return data
        else:
            fileObj.close()
#            logging.getLogger('sysDBLoadFromFile').debug('data = %s'%binascii.b2a_hex(data))
            mylog.info('Load %s cnt = %s'%(fileName, rowCnt - 1))
            
            buf = ''
            buf = CommFunc.WriteDWORD(buf, rowCnt - 1)
            buf = CommFunc.WriteString(buf, len(data), data)
            return buf
    except IOError, (errno, stderr):
        mylog.error('Can not open %s!errno = %s stderr = %s'%(fileName, errno, stderr))
    
    data = ''
    data = CommFunc.WriteDWORD(data, 0)
    return data
        
def SysDBUpdate(className, collection, fileName):
    collection.drop()
    try:
        fileObj = open(fileName,'rb', 0)
        try:
            rowObj = className()
            row = 0
            for line in fileObj:
                row += 1
                if row == 1:
                    continue    #skip header
#                print 'line_%d=%s'%(Col,line)
                rowObj.readLine(line)
                rowObj.adoInsert(collection)
        except IOError,(readErrNo, readStdErr):
            mylog.error('Read %s failed!errno = %s stderr = %s'%(fileName, readErrNo, readStdErr))
        finally:
            fileObj.close()
    except IOError,(errno, stderr):
        mylog.error('Can not open %s! errno = %s stderr = %s'%(fileName, errno, stderr))
        
def seq(db, collectionName, fieldName, feed, increment):
    collection = db['%s_seq'%collectionName]
    resultObj = collection.find_and_modify(query={'_id':fieldName}, update={'$inc':{'seq':increment}}, new=True)
    if resultObj:
        result = resultObj['seq']
    else:
        resultObj = collection.find_and_modify(query={'_id':fieldName}, update={'$set':{'seq':feed}}, new=True,
                                               upsert=True)
        if resultObj:
            result = resultObj['seq']
        else:
            mylog.error('seq failed!')
            return 0
    return result