//int nRecevieSum = 0; private void OnDataReceived(IAsyncResult asyn) { try { SocketPacket theSockId = (SocketPacket)asyn.AsyncState; Byte[] buffer = theSockId.dataBuffer; int iRx = theSockId.thisSocket.EndReceive(asyn); //nRecevieSum += iRx; //SetError(ErrorType.Fail_SendSocket, nRecevieSum.ToString()); // Temp //SetError(ErrorType.Fail_ReceiveSocket, iRx.ToString()); MemoryStream ms = new MemoryStream(buffer, false); BinaryReader br = new BinaryReader(ms); while (iRx > 0) { if (_CompleteBuffer == null) { HeaderInfo headerInfo = (HeaderInfo)BaseInfo.CreateInstance(br); if (headerInfo == null) { break; } if (headerInfo._Header != HeaderInfo.Marker) { break; } _CompleteCount = headerInfo._BodySize; _ReceiveCount = iRx - headerInfo.GetSize(); if (_CompleteCount > _ReceiveCount) { _CompleteBuffer = new byte[_CompleteCount + 1]; for (int i = 0; i < _ReceiveCount; i++) { _CompleteBuffer[i] = br.ReadByte(); } break; } iRx -= headerInfo.GetSize(); iRx -= _CompleteCount; } else { int readCount = _CompleteCount - _ReceiveCount; if (readCount > iRx) { readCount = iRx; } for (int i = 0; i < readCount; i++) { _CompleteBuffer[_ReceiveCount + i] = br.ReadByte(); } _ReceiveCount += readCount; if (_CompleteCount > _ReceiveCount) { break; } iRx -= readCount; } if (_CompleteBuffer != null) { MemoryStream memoryStream = new MemoryStream(_CompleteBuffer, false); BinaryReader binaryReader = new BinaryReader(memoryStream); HandleEvent(binaryReader); binaryReader.Close(); memoryStream.Close(); } else { HandleEvent(br); } } br.Close(); ms.Close(); WaitForData(); Thread.Sleep(0); } catch (ObjectDisposedException) { //SetError(ErrorType.Fail_ReceiveSocket, "OnDataReceived: Socket has been closed"); //_NotifyHandler(NotifyType.Notify_Socket, null, null); //_ClientSocket.Shutdown(SocketShutdown.Both); //_ClientSocket.Disconnect(true); //_ClientSocket.Close(); //_ClientSocket.Dispose(); //_ClientReconnect = new Thread(Reconnect); //_ClientReconnect.Start(); } catch (SocketException ex) { SetError(ErrorType.Fail_ReceiveSocket, "소켓수신 실패: 소켓을 열수 없습니다." + ex.ToString()); //try //{ // _ClientSocket.Shutdown(SocketShutdown.Both); // _ClientSocket.Disconnect(true); // _ClientSocket.Close(); // _ClientSocket.Dispose(); // _ClientSocket = null; //} //catch (Exception ex1) //{ // SetError(ErrorType.Fail_CoonectSocket, ex1.ToString()); //} //Thread.Sleep(1000); //if (_ClientReconnect != null) //{ // _ClientReconnect.Abort(); // _ClientReconnect = null; //} //_ClientReconnect = new Thread(Reconnect); //_ClientReconnect.Start(); } catch { } }
public void OnDataReceived(IAsyncResult asyn)//For Server Mode { lock (_objLockMain) { KeyValuePair aKeyValuePair = (KeyValuePair)asyn.AsyncState; Socket socket = aKeyValuePair._Socket; Byte[] buffer = aKeyValuePair._DataBuffer; try { int iRx = socket.EndReceive(asyn); if (iRx == 0 && socket.Available == 0) { socket.Shutdown(SocketShutdown.Both); socket.Disconnect(true); socket.Close(); _NotifyHandler(NotifyType.Notify_Socket, socket, null); } MemoryStream ms = new MemoryStream(buffer, false); BinaryReader br = new BinaryReader(ms); // Temp //SetError(ErrorType.Fail_ReceiveSocket, iRx.ToString()); SocketBuffer socketBuffer = FindBuffer(socket); while (iRx > 0) { if (socketBuffer._CompleteBuffer == null) { HeaderInfo headerInfo = (HeaderInfo)BaseInfo.CreateInstance(br); if (headerInfo == null) { break; } if (headerInfo._Header != HeaderInfo.Marker) { break; } socketBuffer._CompleteCount = headerInfo._BodySize; socketBuffer._ReceiveCount = iRx - headerInfo.GetSize(); if (socketBuffer._CompleteCount > socketBuffer._ReceiveCount) { socketBuffer._CompleteBuffer = new byte[socketBuffer._CompleteCount + 1]; for (int i = 0; i < socketBuffer._ReceiveCount; i++) { socketBuffer._CompleteBuffer[i] = br.ReadByte(); } break; } iRx -= headerInfo.GetSize(); iRx -= socketBuffer._CompleteCount; } else { int readCount = socketBuffer._CompleteCount - socketBuffer._ReceiveCount; if (readCount > iRx) { readCount = iRx; } for (int i = 0; i < readCount; i++) { socketBuffer._CompleteBuffer[socketBuffer._ReceiveCount + i] = br.ReadByte(); } socketBuffer._ReceiveCount += readCount; if (socketBuffer._CompleteCount > socketBuffer._ReceiveCount) { break; } iRx -= readCount; } if (socketBuffer._CompleteBuffer != null) { MemoryStream memoryStream = new MemoryStream(socketBuffer._CompleteBuffer, false); BinaryReader binaryReader = new BinaryReader(memoryStream); HandleEvent(binaryReader, socket); binaryReader.Close(); memoryStream.Close(); } else { HandleEvent(br, socket); } } br.Close(); ms.Close(); WaitForData(socket); System.Threading.Thread.Sleep(0); } catch (Exception e) { SetError(ErrorType.Fail_ReceiveSocket, "소켓수신 실패: 소켓을 열수 없습니다." + e.ToString()); //socket.Shutdown(SocketShutdown.Both); //socket.Disconnect(true); //socket.Close(); //_NotifyHandler(NotifyType.Notify_Socket, socket, null); } } }
public static BaseInfo CreateInstance(BinaryReader br) { BaseInfo baseInfo = null; InfoType infoType = (InfoType)DecodeInteger(br); switch (infoType) { case InfoType.Result: baseInfo = new ResultInfo(); break; case InfoType.Header: baseInfo = new HeaderInfo(); break; case InfoType.Home: baseInfo = new HomeInfo(); break; case InfoType.User: baseInfo = new UserInfo(); break; case InfoType.UserDetail: baseInfo = new UserDetailInfo(); break; case InfoType.Room: baseInfo = new RoomInfo(); break; case InfoType.RoomList: baseInfo = new RoomListInfo(); break; case InfoType.RoomDetail: baseInfo = new RoomDetailInfo(); break; case InfoType.String: baseInfo = new StringInfo(); break; case InfoType.Voice: baseInfo = new VoiceInfo(); break; case InfoType.Video: baseInfo = new VideoInfo(); break; case InfoType.Present: baseInfo = new IconInfo(); break; case InfoType.Give: baseInfo = new GiveInfo(); break; case InfoType.Game: baseInfo = new GameInfo(); break; case InfoType.GameList: baseInfo = new GameListInfo(); break; case InfoType.GameDetail: baseInfo = new GameDetailInfo(); break; case InfoType.ChatHistory: baseInfo = new ChatHistoryInfo(); break; case InfoType.PointHistory: baseInfo = new PointHistoryInfo(); break; case InfoType.ChargeHistory: baseInfo = new ChargeHistoryInfo(); break; case InfoType.EvalHistory: baseInfo = new EvalHistoryInfo(); break; case InfoType.PresentHistory: baseInfo = new PresentHistoryInfo(); break; case InfoType.GameHistory: baseInfo = new GameHistoryInfo(); break; case InfoType.UserList: baseInfo = new UserListInfo(); break; case InfoType.AskChat: baseInfo = new AskChatInfo(); break; case InfoType.Board: baseInfo = new BoardInfo(); break; case InfoType.Betting: baseInfo = new BettingInfo(); break; case InfoType.Table: baseInfo = new TableInfo(); break; case InfoType.Sicbo: baseInfo = new SicboInfo(); break; case InfoType.Dice: baseInfo = new DiceInfo(); break; case InfoType.DzCard: baseInfo = new DzCardInfo(); break; case InfoType.Horse: baseInfo = new HorseInfo(); break; case InfoType.BumperCar: baseInfo = new BumperCarInfo(); break; case InfoType.Fish: baseInfo = new FishInfo(); break; case InfoType.FishSend: baseInfo = new FishSendInfo(); break; case InfoType.AddScore: baseInfo = new AddScoreInfo(); break; case InfoType.SendCard: baseInfo = new SendCardInfo(); break; case InfoType.Musice: baseInfo = new MusiceInfo(); break; case InfoType.MusiceState: baseInfo = new MusiceStateInfo(); break; case InfoType.ClassInfo: baseInfo = new ClassInfo(); break; case InfoType.ClassList: baseInfo = new ClassListInfo(); break; case InfoType.ClassTypeInfo: baseInfo = new ClassTypeInfo(); break; case InfoType.ClassTypeList: baseInfo = new ClassTypeListInfo(); break; case InfoType.ClassPictureDetail: baseInfo = new ClassPictureDetailInfo(); break; case InfoType.RoomValue: baseInfo = new RoomPrice(); break; case InfoType.Payment: baseInfo = new PaymentInfo(); break; case InfoType.DWGameFile: baseInfo = new DWGameFileInfo(); break; case InfoType.UserState: baseInfo = new UserState(); break; case InfoType.Server: baseInfo = new ServerInfo(); break; case InfoType.AVMsg: baseInfo = new AVMsg(); break; } if (baseInfo != null) { baseInfo.FromBytes(br); } return(baseInfo); }
// 2013-12-27: GreenRose //public void SocketTimer(object obj, System.Timers.ElapsedEventArgs e) //{ // if (_listSocketInfo.Count > 0 && _bSendFinished) // { // SendSocketInfo(_listSocketInfo[0].socket, _listSocketInfo[0].notifyType, _listSocketInfo[0].baseInfo); // } //} // 2013-12-27: GreenRose //private bool SendSocketInfo(Socket socket, NotifyType notifyType, BaseInfo sendInfo) //{ // _bSendFinished = false; // try // { // if (socket.Connected == false) // auto // return true; // int sendSize = 4 + sendInfo.GetSize(); // HeaderInfo headerInfo = new HeaderInfo(); // headerInfo._BodySize = sendSize; // byte[] btBuffer = new byte[headerInfo.GetSize() + sendSize]; // MemoryStream ms = new MemoryStream(btBuffer, true); // BinaryWriter bw = new BinaryWriter(ms); // headerInfo.GetBytes(bw); // EncodeInteger(bw, (int)notifyType); // sendInfo.GetBytes(bw); // bw.Close(); // ms.Close(); // socket.Send(btBuffer); // _listSocketInfo.RemoveAt(0); // _bSendFinished = true; // } // catch (Exception e) // { // SetError(ErrorType.Fail_SendSocket, "소켓송신 실패: " + e.Message); // return false; // } // return true; //} public bool Send(Socket socket, NotifyType notifyType, BaseInfo sendInfo) { //SocketInfo socketInfo = new SocketInfo(); //socketInfo.socket = socket; //socketInfo.notifyType = notifyType; //socketInfo.baseInfo = sendInfo; //_listSocketInfo.Add(socketInfo); try { if (socket.Connected == false) // auto { return(true); } int sendSize = 4 + sendInfo.GetSize(); HeaderInfo headerInfo = new HeaderInfo(); headerInfo._BodySize = sendSize; byte[] btBuffer = new byte[headerInfo.GetSize() + sendSize]; MemoryStream ms = new MemoryStream(btBuffer, true); BinaryWriter bw = new BinaryWriter(ms); headerInfo.GetBytes(bw); EncodeInteger(bw, (int)notifyType); sendInfo.GetBytes(bw); bw.Close(); ms.Close(); //int nSendBuffersize = 0; //while (nSendBuffersize < btBuffer.Length) //{ // nSendBuffersize += socket.Send(btBuffer, nSendBuffersize, btBuffer.Length, SocketFlags.None); //} //if (notifyType == NotifyType.Reply_RoomDetail) //{ // System.Threading.Thread.Sleep(1000); //} SocketBuffer socketBuffer = FindBuffer(socket); socketBuffer._SendBuffers.Add(btBuffer); if (socketBuffer._SendBuffers.Count == 1) { SendAsync(socket); } //System.DateTime dateTime1 = DateTime.Now; //byte[] tempBuffer = new byte[100000]; //for (int i = 0; i < 10; i++) //{ // int nResult = socket.Send(tempBuffer); // if (nResult != 10000) // { // SetError(ErrorType.Fail_SendSocket, "10000 fail"); // } //} //int nSum = 0; //DateTime dateTime2 = DateTime.Now; //double nSecond = (dateTime2 - dateTime1).TotalMilliseconds; } catch (Exception e) { SetError(ErrorType.Fail_SendSocket, "소켓송신 실패: " + e.Message); socket.Shutdown(SocketShutdown.Both); socket.Disconnect(true); socket.Close(); _NotifyHandler(NotifyType.Notify_Socket, socket, null); return(false); } return(true); }