public void sendBoardMsg_Req() { int nChan = 40; string strNotice = null; msgStruct = new CMsg((short)protocol.GM_NOTICE_REQ); msgStruct.WriteData(System.Text.Encoding.Default.GetBytes(strNotice), strNotice.Length); byte[] ret = new byte[] { (byte)nChan, (byte)(nChan >> 8) }; msgStruct.WriteData(ret, 4); client.Send(msgStruct.GetBuf(), 0, msgStruct.GetSize(), 0); for (int i = 0; i < nChan; i++) { ulong itemdata = 0; ret = new byte[] { (byte)LOWORD(itemdata), (byte)(LOWORD(itemdata) >> 8) }; msgStruct.WriteData(ret, 2); ret = new byte[] { (byte)HIWORD(itemdata), (byte)(HIWORD(itemdata) >> 8) }; msgStruct.WriteData(ret, 2); } msgStruct.writeLength(msgStruct.GetSize()); client.Send(msgStruct.GetBuf(), 0, msgStruct.GetSize(), 0); }
public void sendBoardMsg_Req(int[,] iArrayPC, string sNotcie) { int nChan = iArrayPC.GetLength(0); string strNotice = sNotcie; msgStruct = new CMsg((short)protocol.GM_NOTICE_REQ); msgStruct.WriteData(System.Text.Encoding.Default.GetBytes(strNotice), strNotice.Length); byte[] ret = new byte[] { (byte)nChan, (byte)(nChan >> 8) }; msgStruct.WriteData(ret, 2); for (int i = 0; i < nChan; i++) { //星球 ret = new byte[] { (byte)iArrayPC[i, 0], (byte)(nChan >> 8) }; msgStruct.WriteData(ret, 2); //频道 ret = new byte[] { (byte)iArrayPC[i, 1], (byte)(nChan >> 8) }; msgStruct.WriteData(ret, 2); } msgStruct.writeLength(msgStruct.GetSize()); client.Send(msgStruct.GetBuf(), 0, msgStruct.GetSize(), 0); }
/*接收消息 * */ public int receiveMsg() { byte[] recvBuf = new byte[4096]; short size = 0; try { if (client.Receive(recvBuf, 0, 2, 0) != 2) { return(0); } size = (short)CMsg.toInteger(recvBuf, 2); if (size > 0) { if (size > CMsg.NETWORK_MSG_SIZE) { Console.WriteLine("Message Too Large"); return(0); } if (client.Receive(recvBuf, 2, size - 2, 0) == size - 2) { System.Array.Copy(recvBuf, msgStruct.GetBuf(), size); msgStruct.Clear(); return(size); } } } catch (System.Exception ex) { Console.WriteLine(ex.Message); return(0); } return(0); }
public void ReadThreadFunc() { while (true) { if (receiveMsg() > 0) { parseMsg(); msgStruct = new CMsg((short)protocol.GM_CHANNELLIST_REQ); msgStruct.writeLength(msgStruct.GetSize()); client.Send(msgStruct.GetBuf(), 0, msgStruct.GetSize(), 0); Thread.Sleep(1000); } } }
public void connect_Req(string serverIP) { string userName = "******"; string password = "******"; short version = 1; short version1 = 6; int port = 15010; try { IPAddress ipAdress = IPAddress.Parse(serverIP); client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Console.WriteLine("Establishing Connection to {0}",serverIP); IPEndPoint ipReomtePoint = new IPEndPoint(ipAdress, port); client.Connect(ipReomtePoint); if (client.Connected == true) { //Console.WriteLine("Connection established"); msgStruct = new CMsg((short)protocol.GW_LOGIN_REQ); msgStruct.WriteData(System.Text.Encoding.Default.GetBytes(userName), userName.Length); msgStruct.WriteData(System.Text.Encoding.Default.GetBytes(password), password.Length); byte[] ret = new byte[] { (byte)version, (byte)(version >> 8) }; msgStruct.WriteData(ret, 1); ret = new byte[] { (byte)version1, (byte)(version1 >> 8) }; msgStruct.WriteData(ret, 1); msgStruct.writeLength(msgStruct.GetSize()); client.Send(msgStruct.GetBuf(), 0, msgStruct.GetSize(), 0); ThreadStart wokerStart = new ThreadStart(ReadThreadFunc); Thread workThread = new Thread(wokerStart); workThread.Start(); } } catch (SocketException ex) { //Console.WriteLine(ex.Message); MessageBox.Show(ex.Message); } }
int send_msg(CMsg msg) { return(client.Send(msg.GetBuf(), msg.GetSize(), 0)); }
/*消息解析 * */ void parseMsg() { switch (msgStruct.ID()) { case protocol.GW_LOGIN_ACK: { int res = 0; //m_msg >> res; if (res == (int)protocol.RET_OK) { //Console.WriteLine("登录成功"); //发送频道列表请求消息 msgStruct = new CMsg((short)protocol.GM_CHANNELLIST_REQ); msgStruct.writeLength(msgStruct.GetSize()); client.Send(msgStruct.GetBuf(), 0, msgStruct.GetSize(), 0); } else { Console.WriteLine(lg.SDOAPI_SDONoticeInfoAPI_LoginFailure); } } break; case protocol.GM_CHANNELLIST_ACK: { int nChan = 0; byte bHasMore = 0; short wPlanetID = 0, wChannelID = 0; int iLimitUser = 0, iCurrentUser = 0; string ipaddr = ""; nChan = (int)msgStruct.ReadData(nChan, 4); bHasMore = Convert.ToByte(msgStruct.ReadData(bHasMore, 1)); //Console.WriteLine("收到频道信息"+nChan+"个"); for (int i = 0; i < nChan; i++) { try { //星球ID wPlanetID = Convert.ToInt16(msgStruct.ReadData(wPlanetID, 2)); //频道ID wChannelID = Convert.ToInt16(msgStruct.ReadData(wChannelID, 2)); //最大限制人数 iLimitUser = (int)msgStruct.ReadData(iLimitUser, 4); //当前人数 iCurrentUser = (int)msgStruct.ReadData(iCurrentUser, 4); //IP地址 ipaddr = Convert.ToString(msgStruct.ReadData(ipaddr, 15)); //Console.WriteLine(wPlanetID+"/"+wChannelID+"/"+iLimitUser+"/"+iCurrentUser+"/"+ipaddr+"\n"); NoticeDataInfo.InsertChannel_Req(wPlanetID, wChannelID, iLimitUser, iCurrentUser, ipaddr); } catch (System.Exception ex) { Console.WriteLine(ex.Message); } } listCnt++; } break; case protocol.EH_ALIVE_REQ: { msgStruct = new CMsg((short)protocol.EH_ALIVE_ACK); send_msg(msgStruct); } break; default: //Console.WriteLine("Unknown Message Received:"+msgStruct.ID()); break; } }
//发送公告消息 public Message SendBoardInfo() { string serverIP = null; string channelList = null; string boardMessage = null; int userbyID = 0; int result = -1; try { serverIP = Encoding.Default.GetString(msg.m_packet.m_Body.getTLVByTag(TagName.SDO_ServerIP).m_bValueBuffer); //频道列表 channelList = Encoding.Default.GetString(msg.m_packet.m_Body.getTLVByTag(TagName.SDO_ChannelList).m_bValueBuffer); //公告信息 boardMessage = Encoding.Default.GetString(msg.m_packet.m_Body.getTLVByTag(TagName.SDO_BoardMessage).m_bValueBuffer); //操作员ID TLV_Structure tlvStrut = new TLV_Structure(TagName.UserByID, 3, msg.m_packet.m_Body.getTLVByTag(TagName.UserByID).m_bValueBuffer); userbyID = (int)tlvStrut.toInteger(); connect_Req(serverIP); //发公告消息 msgStruct = new CMsg((short)protocol.GM_NOTICE_REQ); msgStruct.WriteData(System.Text.Encoding.Default.GetBytes(boardMessage), boardMessage.Length); string[] chanList = channelList.Split(';'); int nChan = chanList.Length; byte[] ret = new byte[4]; ret = BitConverter.GetBytes(nChan); for (int i = 0; i < nChan; i++) { string[] wPlanetList = chanList[i].Split('/'); short wPlanetID = Convert.ToInt16(wPlanetList[0]); short wChannelID = Convert.ToInt16(wPlanetList[1]); ret = new byte[] { (byte)wPlanetID, (byte)(wPlanetID >> 8) }; msgStruct.WriteData(ret, 2); ret = new byte[] { (byte)wChannelID, (byte)(wChannelID >> 8) }; msgStruct.WriteData(ret, 2); } msgStruct.writeLength(msgStruct.GetSize()); client.Send(msgStruct.GetBuf(), 0, msgStruct.GetSize(), 0); client.Close(); result = NoticeDataInfo.BoardMessage_Req(userbyID, serverIP, channelList, boardMessage); if (result == 1) { SqlHelper.log.WriteLog(lg.API_Display + lg.SDOAPI_SDO + "+>" + lg.API_CommonAPI_ServerIP + CommonInfo.serverIP_Query(serverIP) + lg.SDOAPI_SDONoticeInfoAPI_SendNoticeInfo + lg.API_Success + "!"); Console.WriteLine(DateTime.Now + " - " + lg.API_Display + lg.SDOAPI_SDO + "+>" + lg.API_CommonAPI_ServerIP + CommonInfo.serverIP_Query(serverIP) + lg.SDOAPI_SDONoticeInfoAPI_SendNoticeInfo + lg.API_Success + "!"); return(Message.COMMON_MES_RESP("SUCESS", Msg_Category.SDO_ADMIN, ServiceKey.SDO_BOARDMESSAGE_REQ_RESP)); } else { SqlHelper.log.WriteLog(lg.API_Display + lg.SDOAPI_SDO + "+>" + lg.API_CommonAPI_ServerIP + CommonInfo.serverIP_Query(serverIP) + lg.SDOAPI_SDONoticeInfoAPI_SendNoticeInfo + lg.API_Failure + "!"); Console.WriteLine(DateTime.Now + " - " + lg.API_Display + lg.SDOAPI_SDO + "+>" + lg.API_CommonAPI_ServerIP + CommonInfo.serverIP_Query(serverIP) + lg.SDOAPI_SDONoticeInfoAPI_SendNoticeInfo + lg.API_Failure + "!"); return(Message.COMMON_MES_RESP("FAILURE", Msg_Category.SDO_ADMIN, ServiceKey.SDO_BOARDMESSAGE_REQ_RESP)); } } catch (System.Exception ex) { Console.WriteLine(ex.Message); SqlHelper.log.WriteLog(lg.API_Display + lg.SDOAPI_SDO + "+>" + lg.API_CommonAPI_ServerIP + CommonInfo.serverIP_Query(serverIP) + lg.SDOAPI_SDONoticeInfoAPI_SendNoticeInfo + lg.API_Failure + "!"); Console.WriteLine(DateTime.Now + " - " + lg.API_Display + lg.SDOAPI_SDO + "+>" + lg.API_CommonAPI_ServerIP + CommonInfo.serverIP_Query(serverIP) + lg.SDOAPI_SDONoticeInfoAPI_SendNoticeInfo + lg.API_Failure + "!"); return(Message.COMMON_MES_RESP("FAILURE", Msg_Category.SDO_ADMIN, ServiceKey.SDO_BOARDMESSAGE_REQ_RESP)); } }